package com.resume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.resume.bean.PageBean;
import com.resume.domain.Country;
import com.resume.domain.CountryQuery;
import com.resume.mapper.CountryMapper;
import com.resume.service.CountryService;
import com.resume.utils.RedisUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 梁云亮
 * @Date: 2021/7/31 6:04
 * @Describe:
 */
@Service
public class CountryServiceImpl extends ServiceImpl<CountryMapper, Country> implements CountryService {

    @Resource
    private RedisUtil<List<Country>> redisUtil;

    @Override
    public List<Country> listCountry(CountryQuery countryQuery) {
        List<Country> countryList;
        if (redisUtil.hHasKey("hash_country", countryQuery.getCityId() + "")) {
            //如果缓存中存在，直接从缓存中取
            countryList = redisUtil.hget("hash_country", countryQuery.getCityId() + "");
        } else {
            QueryWrapper<Country> queryWrapper = new QueryWrapper<>();
            if (countryQuery != null) {
                if (countryQuery.getName() != null) {
                    queryWrapper.eq("name", countryQuery.getName());
                }
                if (countryQuery.getStatus() != null) {
                    queryWrapper.eq("status", countryQuery.getStatus());
                }
                if (countryQuery.getCityId() != null) {
                    queryWrapper.eq("city_id", countryQuery.getCityId());
                }
            }
            countryList = baseMapper.selectList(queryWrapper);
            //将查询出来的数据放到缓存中
            redisUtil.hset("hash_country", countryQuery.getCityId() + "", countryList);
        }

        return countryList;
    }

    @Override
    public PageBean<Country> pageCountry(CountryQuery countryQuery) {
        return null;
    }
}
